﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LoopTwoBall : MonoBehaviour
{
    public Text[] btnList;
    public int[] curData = null;

    public void SetData(int[] data)
    {
        curData = data;
    }

    public GameObject[] childs;

    public Action<bool> onSelect;

    private void Awake()
    {
        var trans = transform.GetChild(0);
        childs = new GameObject[trans.childCount];

        for (int i = 0; i < trans.childCount; i++)
        {
            childs[i] = trans.GetChild(i).gameObject;
        }
    }

    public bool select;

    public void OnInit()
    {
        for (int i = 0; i < childs.Length; i++)
        {
            if (i >= curData.Length)
            {
                childs[i].SetActive(false);
            }
            else
            {
                childs[i].transform.GetChild(0).GetComponent<Text>().text = curData[i].ToString();
            }
        }

        GetComponent<UICompos>()["Toggle"].GetComponent<Toggle>().onValueChanged.RemoveAllListeners();
        GetComponent<UICompos>()["Toggle"].GetComponent<Toggle>().isOn = @select;
        GetComponent<UICompos>()["Toggle"].GetComponent<Toggle>().onValueChanged.AddListener((status) =>
        {
            select = status;
            onSelect?.Invoke(@select);
        });
    }
}